package com.tjec.project.msg.mobset.sms;

import com.tjec.framework.config.SmsConfig;
import com.tjec.project.msg.mobset.bean.DataObjectBean;
import com.tjec.project.msg.mobset.bean.msmResultBean;
import com.tjec.project.msg.mobset.factory.DataObjectFactory;
import com.tjec.project.msg.tempuri.MobileListGroup;
import com.tjec.project.msg.tempuri.MobsetApiSoap;
import com.tjec.project.msg.tempuri.holders.ArrayOfSmsIDListHolder;
import com.tjec.project.msg.util.MD5;
import com.tjec.project.msg.util.StringUtils;

import java.net.URL;
import java.rmi.RemoteException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.xml.rpc.holders.LongHolder;
import javax.xml.rpc.holders.StringHolder;

public class sms_Send {
	private static long corpID;
	private static String loginName;
	private static String password;
	private static String serverIP;
    private static	URL url;
	private static String timeStamp = "1105203910";
	private static String addNum;
	private static String timer;
	private static long longSms = 0;
	private static MobileListGroup[] mobileList;
	private static StringHolder errMsg;
	private static ArrayOfSmsIDListHolder smsIDList;
	private static LongHolder count ;
	
	public static msmResultBean SendMsg(String mobiles, String content){
		Date now = new Date(); 
		SimpleDateFormat dateFormat = new SimpleDateFormat("MMddHHmmss");//���Է�����޸����ڸ�ʽ
		timeStamp = dateFormat.format(now);
		errMsg = new StringHolder();
		smsIDList = new ArrayOfSmsIDListHolder();
		count = new LongHolder();
		msmResultBean msmBean = new msmResultBean();
		DataObjectBean bean = DataObjectFactory.getInstance();
		corpID= Long.parseLong(SmsConfig.cordId);
		loginName = SmsConfig.userName;
		password = SmsConfig.passwd;
		serverIP = SmsConfig.serverIP;
		try
		{
		url=new URL(serverIP);
		}
		catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        MobsetApiSoap mobset = DataObjectFactory.getMobsetApi(url);
		String [] mobileArray= StringUtils.replace(mobiles, "；", ";").split(";");
		mobileList = new MobileListGroup[mobileArray.length];
		
		for (int i = 0;i<mobileList.length;i++) {
			mobileList[i] = new MobileListGroup();
			mobileList[i].setMobile(mobileArray[i]);
		}
		MD5 md5 = new MD5();
		password = md5.getMD5ofStr(corpID+password+timeStamp);
		
		try {
			mobset.sms_Send(corpID, loginName, password, timeStamp, addNum, timer, longSms, mobileList, content, count, errMsg, smsIDList);
			msmBean.setErrMsg(errMsg);
			msmBean.setMobileList(mobileList);
			msmBean.setSmsIDList(smsIDList);
			
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return msmBean;
	}
}
